package org.micropayment.consumer.oauth.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import org.springframework.security.AuthenticationException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.oauth.consumer.OAuthConsumerProcessingFilter;

/**
 *
 * @author jj
 */
public class OperationResult extends HttpServlet {

    private static Logger logger =
        LoggerFactory.getLogger(OperationResult.class);
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        AuthenticationException authenticationException =
                (AuthenticationException)request.getSession().getAttribute(
                OAuthConsumerProcessingFilter.OAUTH_FAILURE_KEY);
        String message = "Ok";
        if (authenticationException != null) {
            message = ((AuthenticationException)request.getSession().getAttribute(OAuthConsumerProcessingFilter.OAUTH_FAILURE_KEY)).getMessage();
            logger.debug("Consumer - Resultado operación: " + message);
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>OperationResult</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h3>OperationResult: " + message + "</h3><br/>");
            out.println("<h3>RemoteHost: " + request.getRemoteHost() + "</h3><br/>");
            out.println("<h3>RemoteAddr: " + request.getRemoteAddr() + "</h3><br/>");
            out.println("<h3>LocalAddr: " + request.getLocalAddr() + "</h3><br/>");
            out.println("<h3>QueryString: " + request.getQueryString() + "</h3><br/>");
            out.println("<h3>RequestURI: " + request.getRequestURI() + "</h3><br/>");
            out.println("</body>");
            out.println("</html>");
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
